import 'package:huxiaodui/api/api.dart';
import 'package:huxiaodui/api/model/alarm_model.dart';
import 'package:huxiaodui/components/alarm_list.dart';
import 'package:huxiaodui/view/index/map_page/map_controller.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class AlarmListPage extends StatefulWidget {
  const AlarmListPage({super.key});

  @override
  AlarmListPageState createState() => AlarmListPageState();
}

class AlarmListPageState extends State<AlarmListPage> {
  @override
  void initState() {
    super.initState();
    // 初始化时获取警报
    Api.mobileApp.getAlarms();
  }

  @override
  Widget build(BuildContext context) {

    var alarms = Provider.of<MapController>(context).alarms;
    getAlarms(){
      List<AlarmModel> ret = [];
      for (var value in alarms) {
        if(!value.expired) ret.add(value);
      }
      return ret;
    }

    getExpiredAlarms(){
      List<AlarmModel> ret = [];
      for (var value in alarms) {
        if(value.expired) ret.add(value);
      }
      return ret;
    }


    return DefaultTabController(
      length: 2,
      child: Scaffold(
        appBar: AppBar(
            title: const Text(
              "警报列表",
            ),
            bottom: const TabBar(
              tabs: <Tab>[
                Tab(text: '正在'),
                Tab(text: '过期'),
              ],
            )),
        body: TabBarView(
          children: [
            RefreshIndicator(
              onRefresh: () async {
                await Api.mobileApp.getAlarms();
              },
              child: AlarmList(alarms: getAlarms()),
            ),
            RefreshIndicator(
              onRefresh: () async {
                await Api.mobileApp.getAlarms();
              },
              child: AlarmList(alarms: getExpiredAlarms()),
            ),
          ],
        ),
      ),
    );

  }
}
